package com.zzzy.cont.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzzy.common.vo.PageResult;
import com.zzzy.framework.common.enums.common.NotifyMessageStatusEnum;
import com.zzzy.cont.dao.entity.ConNotifyMessage;
import com.zzzy.cont.dao.mapper.ConNotifyMessageMapper;
import com.zzzy.cont.vo.notify.ConNotifyMessagePageReq;
import com.zzzy.framework.common.util.SecurityUtils;
import com.zzzy.framework.core.mybatis.query.LambdaQueryWrapperE;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
 * 提醒消息
 *
 * @author fc
 * @since 2024-07-25
 */
@Service
public class ConNotifyMessageService extends ServiceImpl<ConNotifyMessageMapper, ConNotifyMessage> {

    public PageResult<ConNotifyMessage> getConNotifyMessagePage(ConNotifyMessagePageReq queryReq) {
        LambdaQueryWrapperE<ConNotifyMessage> queryWrapperE = new LambdaQueryWrapperE<>();
        queryWrapperE.eq(ConNotifyMessage::getUserId, SecurityUtils.getLoginUser().getId())
                .likeIfPresent(ConNotifyMessage::getMessage, queryReq.getMessage())
                .eqIfPresent(ConNotifyMessage::getStatus, queryReq.getStatus())
                .betweenIfPresent(ConNotifyMessage::getCreateTime, queryReq.getBeginTime(), Objects.nonNull(queryReq.getEndTime()) ? queryReq.getEndTime().plusDays(1) : null)
                .orderByDesc(ConNotifyMessage::getId);
        return baseMapper.selectPage(queryReq, queryWrapperE);
    }

    public Long getNotifyMessageCount() {
        return baseMapper.selectCount(new LambdaQueryWrapper<ConNotifyMessage>()
                .eq(ConNotifyMessage::getUserId, SecurityUtils.getLoginUser().getId()).eq(ConNotifyMessage::getStatus, NotifyMessageStatusEnum.NOT_READ.getValue()));
    }

    public void markNotifyMessageRead(Long[] messageIds) {
        if (messageIds.length == 0){
            return;
        }
        Long userId = SecurityUtils.getLoginUser().getId();
        baseMapper.update(new LambdaUpdateWrapper<ConNotifyMessage>().eq(ConNotifyMessage::getUserId, userId).in(ConNotifyMessage::getId, messageIds)
                .set(ConNotifyMessage::getStatus, NotifyMessageStatusEnum.READ.getValue()));
    }

    public void addNotifyMessage(Long conId, Long userId, String message){
        ConNotifyMessage notifyMessage = buildNotifyMessage(conId, userId, message);
        baseMapper.insert(notifyMessage);
    }

    public void addNotifyMessage(Long conId, List<Long> userIdList, String message){
        userIdList.forEach(userId -> addNotifyMessage(conId, userId, message));
    }

    private ConNotifyMessage buildNotifyMessage(Long conId, Long userId, String message){
        ConNotifyMessage notifyMessage = new ConNotifyMessage();
        notifyMessage.setConId(conId);
        notifyMessage.setUserId(userId);
        notifyMessage.setMessage(message);
        notifyMessage.setCreateTime(LocalDateTime.now());
        notifyMessage.setStatus(NotifyMessageStatusEnum.NOT_READ.getValue());
        return notifyMessage;
    }
}
